import svc
import uiconst
import miscUtil
import GameWorld

class EveNavigationService(svc.navigation):
    __guid__ = 'svc.eveNavigation'
    __replaceservice__ = 'navigation'
    __dependencies__ = ['entityClient']
    MOVDIR_FORWARD = 0
    MOVDIR_BACKWARD = 1
    MOVDIR_RIGHT = 2
    MOVDIR_LEFT = 3

    def Run(self, memStream = None):
        svc.navigation.Run(self, memStream)



    def ToggleCameraBoundMovement(self):
        player = self.entityClient.GetPlayerEntity()
        if not player:
            return 
        animController = player.animation.controller
        animController.cameraBoundRotation = (not animController.cameraBoundRotation)



    def _UpdateMovement(self, vkey):
        focus = uicore.registry.GetFocus()
        if (focus and hasattr(focus, 'OnChar')):
            return False
        (fwdKey, backKey, moveLKey, moveRKey,) = self.PrimeNavKeys()
        if (vkey in self.navKeys):
            self.hasControl = True
            if (vkey == fwdKey):
                self.lastForwardBackward = self.MOVDIR_FORWARD
            elif (vkey == backKey):
                self.lastForwardBackward = self.MOVDIR_BACKWARD
            elif (vkey == moveLKey):
                self.lastLeftRight = self.MOVDIR_LEFT
            elif (vkey == moveRKey):
                self.lastLeftRight = self.MOVDIR_RIGHT
            return True



    def RecreatePlayerMovement(self):
        if getattr(self, '_delayedMouseUpStillPending', False):
            return 
        player = self.entityClient.GetPlayerEntity()
        if player:
            if (self.hasControl and uicore.triapp.IsActive()):
                curKeyState = self.GetKeyState()
                (fwdActive, backActive, moveLActive, moveRActive,) = curKeyState
                isPathing = isinstance(player.movement.avatar.GetActiveMoveMode(), GameWorld.PathToMode)
                isKeyPressed = (fwdActive or (backActive or (moveLActive or moveRActive)))
                if (isKeyPressed and isPathing):
                    player.movement.avatar.PushMoveMode(GameWorld.KBMouseMode(0.0))
                    isPathing = False
                y = 0.0
                z = 0.0
                if ((backActive and (fwdActive and (self.lastForwardBackward == self.MOVDIR_BACKWARD))) or (backActive and (not fwdActive))):
                    z = -1.0
                elif fwdActive:
                    z = 1.0
                x = 0.0
                if ((moveLActive and (moveRActive and (self.lastLeftRight == self.MOVDIR_LEFT))) or (moveLActive and (not moveRActive))):
                    x = -1.0
                elif moveRActive:
                    x = 1.0
                if not isPathing:
                    player.movement.avatar.localHeading = (x,
                     y,
                     z)
            else:
                player.movement.avatar.localHeading = (0, 0, 0)



    def _TurnPlayerToCamera(self):
        return 



    def SetMovementKey(self, direction, keyDown = True):
        uicore.triapp.SetActive()
        (fwdKey, backKey, moveLKey, moveRKey,) = self.PrimeNavKeys()
        movementMaps = {'FORWARD': fwdKey,
         'BACKWARD': backKey,
         'LEFT': moveLKey,
         'RIGHT': moveRKey,
         'SHIFT': uiconst.VK_SHIFT}
        uicore.uilib.SetKey(movementMaps[direction], keyDown)



    def GetKeyState(self):
        (fwdKey, backKey, moveLKey, moveRKey,) = self.navKeys
        fwdActive = uicore.uilib.Key(fwdKey)
        backActive = uicore.uilib.Key(backKey)
        moveLActive = uicore.uilib.Key(moveLKey)
        moveRActive = uicore.uilib.Key(moveRKey)
        return (fwdActive,
         backActive,
         moveLActive,
         moveRActive)



    def PrimeNavKeys(self):
        fwdCommand = uicore.cmd.GetShortcutByFuncName('CmdMoveForward')
        backCommand = uicore.cmd.GetShortcutByFuncName('CmdMoveBackward')
        moveLCommand = uicore.cmd.GetShortcutByFuncName('CmdMoveLeft')
        moveRCommand = uicore.cmd.GetShortcutByFuncName('CmdMoveRight')
        self.navKeys = [ each[-1] for each in (fwdCommand,
         backCommand,
         moveLCommand,
         moveRCommand) ]
        return self.navKeys




